﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.INFS;
using Sector7.Medical.Biz;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;

namespace Sector7.Medical.INFM.Windows
{
    /// <summary>
    /// AddSickBedWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddSickBedWindow : AddModeWindowBase
    {
        #region Accessor
        public M7_SickBed SickBed
        {
            get { return m_entity as M7_SickBed; }
            set { m_entity = value; }
        }
        #endregion

        #region Public Functions
        #endregion

        #region Private Functions
        private void AddSickBedWindow_Loaded(object obj, RoutedEventArgs e)
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadData_Worker);
        }

        private void LoadData_Worker(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            GetData();
            this.Dispatcher.BeginInvoke(new DelegateWithNothing(LoadData));
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        private void GetData()
        {
            m_rooList = Agent_INFS.GetSickRoomList();
            m_areaList = Agent_INFS.GetSickAreaList();
            m_bedLevelList = Agent_INFS.GetSickBedLevelList();
        }

        /// <summary>
        /// 加载所有病床数据
        /// </summary>
        private void LoadData()
        {
            ClientUtil.LoadItemsSource(cbbSickRoom, m_rooList);
            ClientUtil.LoadItemsSource(cbbSickArea, m_areaList);
            ClientUtil.LoadItemsSource(cbbSickBedLevel, m_bedLevelList);

            switch (Crud)
            { 
                case CrudType.Update:
                    InitializeForUpdating();
                    break;
                default:
                    break;
            }
        }

        protected override void InitializeForCreating()
        {
            txbBedId.Text = "";
            txbBedNumber.Text = "";
            cbbSickArea.SelectedIndex = 0;
            cbbSickBedLevel.SelectedIndex = 0;
            cbbSickRoom.SelectedIndex = 0;
            txbRemarks.Text = "";
            ckbIsValid.IsChecked = false;

            base.InitializeForCreating();
        }

        private void InitializeForUpdating()
        {
            txbBedId.Text = SickBed.BedId.ToString();
            txbBedNumber.Text = SickBed.BedNumber;
            cbbSickArea.SelectedValue = SickBed.AreaId;
            cbbSickBedLevel.SelectedValue = SickBed.BedLevelId;
            cbbSickRoom.SelectedValue = SickBed.RoomId;
            txbRemarks.Text = SickBed.Remarks;
            ckbIsValid.IsChecked = SickBed.IsValid;
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            string bedNumber = txbBedNumber.Text;
            string spellCode = txbSpellCode.Text;
            string bedRoom = (cbbSickRoom.SelectedItem as M7_SickRoom).RoomName;
            string bedArea = (cbbSickArea.SelectedItem as M7_SickArea).AreaName;
            string bedLevel = (cbbSickBedLevel.SelectedItem as M7_SickBedLevel).BedLevelName;
            string remarks = txbRemarks.Text;
            bool isVallid = (bool)ckbIsValid.IsChecked;

            if (string.IsNullOrEmpty(bedNumber))
            {
                m_errors.Add("病床号","不能为空!");
            }

            if (string.IsNullOrEmpty(spellCode))
            {
                m_errors.Add("拼音码", "不能为空!");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            string bedId = txbBedId.Text;
            string bedNumber = txbBedNumber.Text;
            string spellCode = txbSpellCode.Text;
            int bedRoomId = (cbbSickRoom.SelectedItem as M7_SickRoom).RoomId;
            int bedAreaId = (cbbSickArea.SelectedItem as M7_SickArea).AreaId;
            int bedLevelId = (cbbSickBedLevel.SelectedItem as M7_SickBedLevel).BedLevelId;
            string remarks = txbRemarks.Text;
            bool isVallid = (bool)ckbIsValid.IsChecked;

            SickBed = new M7_SickBed();
            switch(m_crud)
            {
                case CrudType.Update:
                    SickBed.BedId = Convert.ToInt32(bedId);
                    break;
                default:
                    break;
            }
            SickBed.BedNumber = bedNumber;
            SickBed.RoomId = bedRoomId;
            SickBed.BedLevelId = bedLevelId;
            SickBed.AreaId = bedAreaId;
            SickBed.Remarks = remarks;
            SickBed.IsValid = isVallid;

            base.AddData();
        }

        /// <summary>
        /// 生成拼音码
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void btnSpellCode_Click(object obj, RoutedEventArgs e)
        {
            txbSpellCode.Text = CharacterUtil.GetSpellCodeString(txbBedNumber.Text.Trim());
        }
        #endregion

        #region Structure
        public AddSickBedWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner Members
        /// <summary>
        /// 获取所有病房列表
        /// </summary>
        private List<M7_SickRoom> m_rooList;

        /// <summary>
        /// 获取所有病区列表
        /// </summary>
        private List<M7_SickArea> m_areaList;

        /// <summary>
        /// 获取所有病房等级信息列表
        /// </summary>
        private List<M7_SickBedLevel> m_bedLevelList;

        /// <summary>
        /// 
        /// </summary>
        private List<M7_SickBed> m_bedList;
        #endregion
    }
}
